#! /usr/bin/env python
#coding:utf-8
import datetime,time,re
from scrapy.utils.python import  unicode_to_str
class dateutil(object):
    ds = datetime.datetime.now()
 
    def day_get(self,d=ds):
        oneday = datetime.timedelta(days=1)
        day = d - oneday
        date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)
        date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59)
        print '---'.join([str(date_from), str(date_to)])
 
    def week_get(self,d=ds):
        dayscount = datetime.timedelta(days=d.isoweekday())
        dayto = d - dayscount
        sixdays = datetime.timedelta(days=6)
        dayfrom = dayto - sixdays
        date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0)
        date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
        print '---'.join([str(date_from), str(date_to)])
 
    def month_get(self,d=ds):
        dayscount = datetime.timedelta(days=d.day)
        dayto = d - dayscount
        date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0)
        date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
        print '---'.join([str(date_from), str(date_to)])
      
    def  start_time(self,d=ds):
        return datetime.datetime.strftime(d,'%Y-%m-%d')
        
    def to_time(self,d=ds):
        return datetime.datetime.strftime((d-datetime.timedelta(days=30)),'%Y-%m-%d')
    # 指定格式的字符串转换为 时间戳
    def date_timestamp(self,datestr):
        return int(time.mktime(time.strptime(datestr,'%Y-%m-%d %H:%M:%S')))
    def to_timestramp(self,datestr):
        return int(time.mktime(time.strptime(datestr,'%Y-%m-%d %H:%M')))
    # 2015年12月8日
    def get_timestramp(self,date):
        return int(time.mktime(time.strptime(date,'%Y-%m-%d')))
    #给定时间戳 转化为标准的 日期时间 格式 字符串
    def to_localdatestr(self,timestramp):
        return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(timestramp))
    #得到当前时间的 标准 日期时间格式 字符串
    def get_localtimestamp(self):
        return (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
    #得到 当前时间 之前 给定的天数，小时数，秒数   的时间戳
    def get_timestramp_by_day(self,day,hour,second):
        temp= (datetime.datetime.now() - datetime.timedelta(days =day,hours=hour,seconds=second))
        #return temp.strftime("%Y-%m-%d %H:%M:%S")
        return int(time.mktime(temp.timetuple()))
    
    #特殊用途的时间转换
    def get_timestramp_by_params(self,datestr):
        if  re.search("天",datestr) is not None:
            day=re.search("\d+",datestr).group(0)
            return self.get_timestramp_by_day(int(day),0,0)
        elif re.search("小时",datestr) is not None:
            hour=re.search("\d+",datestr).group(0)
            return self.get_timestramp_by_day(0,int(hour),0)
        elif re.search("分钟",datestr) is not None:
            second=int(re.search("\d+",datestr).group(0))*60
            return self.get_timestramp_by_day(0,0,int(second))
    # datestr=2015年2月11日 08:00
    def get_datetimestr_by_str(self,datestr):
        #return re.sub(u"[\u4e00-\u9fa5]+","-",datestr)
        return re.sub("-\s+"," ",re.sub(u"[\u4e00-\u9fa5]+","-",datestr))
    #特殊用途
    def get_timestramp_by_str(self,datestr):
        return self.to_timestramp(self.get_datetimestr_by_str(datestr))
    
    def get_datetime(self,timestramp):
        times=time.localtime(timestramp)
        return datetime(times[0],times[1],times[2],times[3],times[4],times[5])
    
    @staticmethod
    def get_instance():
        return dateutil();
    
        
if __name__=='__main__':
    
    util=dateutil()
#     a = "2011-09-28 10:00:00"
    s=u"2015年2月11日 08:00"
#     print util.date_timestamp(a);
#     print util.get_localtimestamp()
#     print util.to_localdatestr(1317175200)
#     print util.get_timestramp_by_day(1,1,0)
#     #print re.search("\d+", "7小时前").group(0)
#     print util.get_timestramp_by_params("1分钟前")
#     #print "1"*60
#     print dateutil.get_instance().get_timestramp_by_params("1小时前")
    print dateutil.get_instance().get_timestramp_by_str(s)
#     print dateutil.get_instance().get_timestramp_by_str(s)
#     util.day_get()
#     util.start_time()
#     util.to_time()
#     params={}
#     for dt in range(1,10):
#         params[dt]=dt+1
#     
#     print params
#     print type(params)
#     #'javascript:toPage(100)'
#     m=re.search("(\d+)","javascript:toPage(100)")
#     
#     print m.group(0)
#     url='http://sns.sseinfo.com/ajax/feeds.do?type=11&pageSize=10&lastid=-1&show=1&page=1'
#     print re.search('page=(\d+)',url).group(0)
#     
#     print re.sub('page=(\d+)',"page="+str(3),url)
    
    url="http://news.baidu.com/ns?word=python&pn=0&cl=2&ct=1&tn=news&rn=20&ie=utf-8&bt=0&et=0&rsv_page=1"
    param=re.search("\d+",re.search("pn=\d+",url).group(0)).group(0)
    print param
    print re.sub("pn=\d+","pn="+str(1),url)
    
    
    ss=u"新浪新闻  2015年11月24日 08:18"
    time_publish=u"凤凰网  2015年11月25日 "
    print time_publish.split(' ')
    #print util.to_timestramp(util.get_datetimestr_by_str(re.search("\d+.+",time_publish).group(0).strip()))
    
    print #util.to_timestramp(util.get_datetimestr_by_str(re.search("\s+\d+.+",time_publish).group(0).strip()))  
    
    print  re.search("\s+\d+.+",time_publish).group(0).strip()
    
    #print util.to_timestramp(util.get_datetimestr_by_str(re.search("\s+\d+.+",ss).group(0).strip()))
    
    print util.to_timestramp(util.get_datetimestr_by_str(re.search("\s+\d+.+", ss).group(0).strip()))
    
    sss="2015-6-20 "
    #print util.get_timestramp(sss)
    url="https://www.google.com.hk/search?q=python&safe=strict&hl=zh-CN&gl=cn&tbm=nws&start=0&dpr=1"
    print re.sub('q=(\w+)',"q="+"熊先华",url)
    
#     times=time.localtime(1447305601)
#     util.get_datetime(1447305601)
    

    
    
    